home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12455 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: argonet.co.uk!argbe15
  2. From: Dave Mullard <dmullard@argonet.co.uk>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to get at base class fields from member functions?
  5. Date: Wed, 20 Mar 1996 02:50:32
  6. Organization: UnipalmPIPEX server (post doesn't reflect views of UnipalmPIPEX
  7. Distribution: world
  8. Message-ID: <internews46B1A3DB3E@argonet.co.uk>
  9. References: <internews46B00D1FBD@argonet.co.uk> <4ihhkf$2ni@news5.erols.com> <internews46B0766104@argonet.co.uk> <marnoldDoGE1H.FMH@netcom.com> <marnoldDoGEGI.G09@netcom.com>
  10. Reply-To: Dave Mullard <dmullard@argonet.co.uk>
  11. NNTP-Posting-Host: an194.du.pipex.com
  12. X-Newsreader: VTi Voyager InterNews 0.15 for Acorn RISC OS (Patched by RA)
  13.  
  14. Matt Arnold <marnold@netcom.com> wrote:
  15.  
  16. >Dave Mullard <dmullard@argonet.co.uk> writes:
  17.  
  18. >>Chris Cobb <ccobb@erols.com> wrote:
  19. >>> Dave Mullard <dmullard@argonet.co.uk> wrote:
  20. >>> >Given three classes A, B and C I want to have multiple As for each B
  21. >>> and
  22. >>> >multiple Bs for each C. To do this I could create the following.
  23. >>> >
  24. >>> >class b1 : public B
  25. >>> >{
  26. >>> >A a1;
  27. >>> >A a2;
  28. >>> >A a3;
  29. >>> >};
  30.  
  31. >>> >class b2 : public B
  32. >>> >{
  33. >>> >A a1;
  34. >>> >A a2;
  35. >>> >A a3;
  36. >>> >A a4;
  37. >>> >A a5;
  38. >>> >};
  39.  
  40. >>> >class c1 : public C
  41. >>> >{
  42. >>> >b1 b1;
  43. >>> >b2 b2;
  44. >>> >};
  45.  
  46. >>> >The question is, how within functions for class B do I access fields
  47. >>> >within class C, and similarly, how within the class A functions do I
  48. >>> >access fields within class B?
  49.  
  50.  
  51. > snip
  52.  
  53. >>I suppose at its simplest level the problem is this
  54.  
  55. >>class B
  56. >>{
  57. >>int fred;
  58. >>   class A
  59. >>   {
  60. >>      A() { };
  61. >>   };
  62. >>A a1;
  63. >>A a2;
  64. >>};
  65.  
  66. >>How within A::A do I refer to fred. 
  67.  
  68. >You don't.  
  69.  
  70. >Realize that, even though A is nested class of B, A and B are distinct 
  71. >types and not "linked" in the way you seen to think they are.  In your 
  72. >sample code above, A has no more connection to or knowledge of B than 
  73. >some other class C in a totally different part of your program.
  74.  
  75. >If you need this kind of link, you must explicitly code it yourself.  
  76. >For example, you could arrange to pass, to an instance of A, a 
  77. >reference or pointer to an instance B, and access B::fred in *that* 
  78. >instance.
  79.  
  80. >A could have the following member...
  81.  
  82. >void A::access_fred(B* b)
  83. >   { 
  84. >   b.fred = 42;
  85. >   }
  86.  
  87. My problem is that I still get myself confused over the relationship
  88. between classes and that between objects. As you say, you need to pass
  89. pointers so I have settled for the following approach.
  90.  
  91. C c1;
  92.  
  93. B b1(&c1);
  94.  
  95. A a11(&b1);
  96. A a12(&b1);
  97. A a13(&b1);
  98.  
  99. B b2(&c1);
  100.  
  101. A a21(&b2);
  102. A a22(&b2);
  103. A a23(&b2);
  104. A a24(&b2);
  105. A a25(&b2);
  106.  
  107. Then each A can have a pointer to its B and the Bs to the C. It isnt text
  108. book c++ but its easy to program. It has the advantage that it works
  109. regardless of whether they are static, automatic or dynamic.
  110.  
  111. BTW my compiler CFront does not fault
  112.  
  113. b1 b1;
  114. B2 b2;
  115. -- 
  116.  Dave Mullard <dmullard@argonet.co.uk>
  117.  
  118.